<!-- 内容主体区域 -->
<link rel="stylesheet" href="layui/css/layui.css">
<style>.layui-table-cell {
    height: 40px;
    line-height: 40px;
}

.layui-table-cell img {
    max-height: 70px;
}</style>
<div style="padding: 15px;">
    <div class="layui-tab">
        <ul class="layui-tab-title">
            <li class="layui-this">篮球球员</li>
        </ul>
        <div class="layui-tab-content">

            <!--            <div class="layui-input-inline">
                            <form class="layui-form" action="">
                                <select id="Team" name="city" lay-verify="" lay-filter="Team">
                                    <option value="">选择球队</option>
                                </select>
                            </form>
                        </div>-->
            <a id="add" class="layui-btn layui-btn-xs" lay-event="add_player">添加球员</a>
            <table class="layui-hide" id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
            </script>
            <script type="text/html" id="show-img">
                <img src="http://svcsa.org/uploads/{{d.PlayerPhotoSrc}}" alt="图片名">
            </script>
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>

    </div>
</div>

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="layui/layui.js"></script>
<script>
    var url = location.search;
    var id;
    var Request = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            Request[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
        }
    }
    id = Request["id"];
    layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        var mycars = []
        var table_data
        table.render({
            elem: '#test',
            url: 'http://svcsa.org/BBPlayer?teamid=' + id,
            parseData: function (res) {
                table_data = res.data
                console.log(res)
                return {
                    "code": 0, //解析接口状态
                    "msg": '', //解析提示文本
                    "count": res.totalcount, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            method: 'get',
            request: {
                pageName: 'page' //页码的参数名称，默认：page
                ,limitName: 'pagesize' //每页数据量的参数名，默认：limit
            },
            limit:20,
            title: '用户数据表',
            cols: [
                [
                    // {type: 'checkbox', fixed: 'left'},
                    {
                    field: 'PlayerID',
                    title: 'ID',
                    width: 60,
                    height: 60,
                    fixed: 'left',
                    unresize: true,
                    sort: true,
                }, {
                    field: 'PlayerName',
                    title: '球员名称',
                    width: 200,
                }, {
                    field: 'Number',
                    title: '球员号码',
                    width: 100,
                }, {
                    field: 'Birth',
                    title: '出生年月日',
                    width: 200,
                }, {
                    title: '操作',
                    fixed: 'right',
                    width: 150,
                    toolbar: '#barDemo'
                }]
            ],
            page: false
        });

        $("#add").click(function () {
            var ly=layer.open({
                type: 2,
                title: '添加球员',
                shadeClose: true,
                shade: 0.8,
                area: ['100%', '100%'],
                content: 'bb_team_player_add.html?id=' + id,
                success: function (layero, index) {
                },
                end: function () {
                    location.reload();
                }
            });
            layer.full(ly)
        })

        table.on('tool(test)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'del') {
                layer.confirm('确认删除', {
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    var mycars = ''
                    for (var i = 0; i < table_data.length; i++) {
                        var t_player_id=table_data[i].PlayerID
                        // console.log(i)
                        if (data.PlayerID !== t_player_id) {
                            if (i === table_data.length - 1) {
                                mycars += table_data[i].PlayerID
                            } else {
                                mycars += table_data[i].PlayerID + ','
                            }
                        }
                    }
                    let index = parent.layer.getFrameIndex(window.name);
                    $.ajax({
                        url: 'http://svcsa.org/bbplayer/setteam',
                        type: 'POST',
                        dataType: 'json',
                        data: {
                            PlayerIDs: mycars,
                            TeamID: id,
                        },
                        header: {
                            Accept: 'application/json'
                        },
                        success: function (data) {
                            console.log("删除成功-----------")
                            console.log(data)
                            layer.msg('删除成功', {
                                icon: 1,
                                time: 1500
                            }, function () {
                                location.reload();
                            });
                        },
                        error: function (data) {
                            console.log(data)
                        }
                    });
                }, function () {
                });
            }
        });

    });
</script>